package com.yycx.module.bus.provider.controller.hy;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yycx.common.mybatis.model.*;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RestController;
import com.yycx.common.springmvc.base.BaseController;
import com.yycx.module.bus.client.entity.hy.BusHyorderInoviceDetails;
import com.yycx.module.bus.provider.service.hy.BusHyorderInoviceDetailsService;


/**
 * 开票明细控制器
 *
 * @author flyme
 * @date 2023-09-17
 */
@RestController
@RequestMapping("/bus/bhid/")
public class BusHyorderInoviceDetailsController extends BaseController<BusHyorderInoviceDetailsService, BusHyorderInoviceDetails> {

    @Autowired
    private SysCompanyService companyService;

    @Autowired
    private BusSalemanService salemanService;

    @ApiOperation(value = "开票明细-分页列表", notes = "开票明细分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return bizService.pageList(params);
    }

    @ApiOperation(value = "开票明细-列表", notes = "开票明细列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return bizService.listEntityMap(params);
    }

    @ApiOperation(value = "开票明细-添加", notes = "添加开票明细")
    @PostMapping(value = "save")
    public ResultBody save(@RequestParam(required = false) Map params) {
        return bizService.add(params);
    }

    @ApiOperation(value = "开票明细-更新", notes = "更新开票明细")
    @PostMapping(value = "update")
    public ResultBody update(@RequestParam(required = false) Map params) {
        return bizService.edit(params);
    }

    @ApiOperation(value = "开票明细-删除", notes = "删除开票明细")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return bizService.delete(params);
    }


    @ApiOperation(value = "开票明细-详情", notes = "开票明细详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        return bizService.get(params);
    }

    @ApiOperation(value = "开票明细-拆分金额", notes = "拆分金额")
    @PostMapping(value = "cfAmount")
    public ResultBody cfAmount(@RequestParam Long invoiceDetailsId, @RequestParam BigDecimal amount) {
        bizService.cfAmount(invoiceDetailsId, amount);
        return ResultBody.ok("拆分成功");
    }


    @ApiOperation(value = "开票明细-撤销拆分金额", notes = "撤销拆分金额")
    @PostMapping(value = "revokeCfAmount")
    public ResultBody revokeCfAmount(@RequestParam Long invoiceDetailsId) {
        return bizService.revokeCfAmount(invoiceDetailsId);
    }

    @ApiOperation(value = "撤销支付id", notes = "撤销支付id")
    @PostMapping(value = "clearPayId")
    public ResultBody clearPayId(@RequestParam Long invoiceDetailsId) {
        bizService.clearPayIdByInvoiceDetailsId(invoiceDetailsId);
        return ResultBody.msg("删除成功");
    }

    @ApiOperation(value = "插入结算申请明细", notes = "插入结算申请明细")
    @PostMapping(value = "insertInvoiceDetails")
    public ResultBody insertInvoiceDetails(@RequestParam String paymentId) {
        bizService.insertInvoiceDetails(paymentId);
        return ResultBody.msg("同步成功");
    }


    @ApiOperation(value = "获取发票设定信息", notes = "获取发票设定信息")
    @GetMapping(value = "getInvoiceInfo")
    public ResultBody getInvoiceInfo(String selectIds) {
        EntityMap map = bizService.getInvoiceInfo(selectIds);
        return ResultBody.ok(map);
    }

    /**
     * 导出数据
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "export")
    public void export(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        String handlerName = MapUtil.getStr(params, "handlerName");
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String fileName = company.getCompanyName() + "-";
        Long saleManId = MapUtil.getLong(params, "saleManId");
        if (FlymeUtils.isNotEmpty(saleManId)) {
            BusSaleman saleman = salemanService.getById(saleManId);
            fileName += saleman.getSaleManName() + "-";
        }
        fileName += MapUtil.getStr(params, "fileName");
        bizService.export(params, request, response, fileName + DateUtil.formatDate(DateUtil.date()), fileName, handlerName);
    }

    /**
     * 导出数据
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "exportTemplate")
    public void exportTemplate(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        String handlerName = MapUtil.getStr(params, "handlerName");
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String fileName = company.getCompanyName() + "-" + MapUtil.getStr(params, "fileName");
        bizService.templateExport(params, request, response, fileName + DateUtil.formatDate(DateUtil.date()), fileName, handlerName);
    }

}
